#include "cTime_t.h"
#include <time.h>
#include<iostream>
#include <iomanip>

using namespace std;


//*****************************************************************************
//default CTOR from current time
//*****************************************************************************
cTime_t::cTime_t()
{
	time_t time_date;
	struct tm* 	current_time;

	time_date = time(0);	
    current_time = localtime(&time_date);

	hours = current_time -> tm_hour;
	minutes = current_time -> tm_min;
	seconds = current_time -> tm_sec;
}


//*****************************************************************************
//copy CTOR
//*****************************************************************************
cTime_t::cTime_t(const cTime_t& t)
{
	hours = t.hours;
	minutes = t.minutes;
	seconds = t.seconds;
}

//*****************************************************************************
//CTOR from hours (0-23)
//*****************************************************************************
cTime_t::cTime_t(const int hr)
{
	if ((hr<0)||(hr>23))
	{
		cout << "ERROR: hours range is from 0 to 23\n";
		exit(1);
	}

	hours = hr;
	minutes = 0;
	seconds = 0;
}

//*****************************************************************************
//CTOR from hours (0-23) and minutes (0-59)
//*****************************************************************************
cTime_t::cTime_t(const int hr, const int min)
{
	if ((hr<0)||(hr>23)||(min<0)||(min>59))
	{
		cout << "ERROR: hours range is from 0 to 23\n";
		cout << "       minutes range is from 0 to 59\n";
		exit(1);
	}

	hours = hr;
	minutes = min;
	seconds = 0;
}

//*****************************************************************************
//CTOR from hours (0-23), minutes (0-59) and seconds (0-59)
//*****************************************************************************
cTime_t::cTime_t(const int hr, const int min, const int sec)
{
	if ((hr<0)||(hr>23)||(min<0)||(min>59)||(sec<0)||(sec>59))
	{
		cout << "ERROR: hours range is from 0 to 23\n";
		cout << "       minutes range is from 0 to 59\n";
		cout << "       seconds range is from 0 to 59\n";
		exit(1);
	}

	hours = hr;
	minutes = min;
	seconds = sec;
}

//*****************************************************************************
//DTOR
//*****************************************************************************
cTime_t::~cTime_t()
{
}

//*****************************************************************************
//assignment operator
//*****************************************************************************
const cTime_t& cTime_t::operator=(const cTime_t& t)
{
	if (this != &t)
	{
		hours = t.getHours();
		minutes = t.getMinutes();
		seconds = t.getSeconds();
	}

	return *this;
}

//*****************************************************************************
//set hours (0-23) in existing object 
//return 1 if succeeded, 0 otherwise
//*****************************************************************************
int cTime_t::setHours(const int hr)
{
	if ((hr<0)||(hr>23))
	{
		cout << "ERROR: hours range is from 0 to 23\n";
		return 0;
	}

	hours = hr;
	return 1;
}

//*****************************************************************************
//set minutes (0-59) in existing object
//return 1 if succeeded, 0 otherwise
//*****************************************************************************
int cTime_t::setMinutes(const int min)
{
	if ((min<0)||(min>59))
	{
		cout << "ERROR: minutes range is from 0 to 59\n";
		return 0;
	}

	minutes = min;
	return 1;
}

//*****************************************************************************
//set seconds (0-59) in existing object
//return 1 if succeeded, 0 otherwise
//*****************************************************************************
int cTime_t::setSeconds(const int sec)
{
	if ((sec<0)||(sec>59))
	{
		cout << "ERROR: seconds range is from 0 to 59";
		return 0;
	}

	seconds = sec;
	return 1;
}

//*****************************************************************************
//print out time object in two formats
//format=1: HH:mm:ss     example: 17:50:10
//format=2: hh:mm:ss tt  example:  5:50:10 PM
//*****************************************************************************
void cTime_t::printOut(const int frmt) const
{
	cout << setfill('0');

	switch (frmt)
	{
	case 1:
		cout << setw(2) << hours << ":" << setw(2) << minutes << ":" 
			                                     << setw(2) << seconds << "\n";
		break;
	case 2:					
		if (hours<12)
			cout << setw(2) << hours << ":" << setw(2) << minutes << ":" 
			                                  << setw(2) << seconds << "am\n";
		else
			cout << setw(2) << (hours-12) << ":" << setw(2) << minutes << ":" 
			                                  << setw(2) << seconds << "pm\n";
		break;
	default:
		cout << "ERROR: format value must be 1 or 2\n";
	}
}

//*****************************************************************************
//get hours
//*****************************************************************************
int cTime_t::getHours() const
{
	return hours;
}

//*****************************************************************************
//get minutes
//*****************************************************************************
int cTime_t::getMinutes() const
{
	return minutes;
}

//*****************************************************************************
//get seconds
//*****************************************************************************
int cTime_t::getSeconds() const
{
	return seconds;
}

//*****************************************************************************
//plus operator overloading
//*****************************************************************************
cTime_t cTime_t::operator+(const cTime_t& t) const
{
	int hr, min, sec;

	sec = seconds + t.seconds;
	min = minutes + t.minutes + sec/60;
	hr = hours + t.hours + min/60;

	sec = sec%60;
	min = min%60;
	hr = hr%24;

	return cTime_t(hr, min, sec);
}